<?php
  class TeamsAction extends BaseAction
  {
      public function executeIndex()
      {
        $PoulGroeps = PoulGroepPeer::getAllGroups();
        core::$tpl->assign('groups', $PoulGroeps);
        $this->setContent('team/teamIndex.tpl');  
      }
      
      public function executeAddGroup()
      {
         
          if(isset($_POST['submit']))
          {
              $obj = new PoulGroep();
              $obj->setNaam($_POST['name']);
              $obj->save();
              
              core::$tpl->assign('message', GROUP_ADDED_SUCCESFULL_MESSAGE);
              $this->executeIndex();  
          }
          else
          {
            $this->setContent('team/teamAddGroup.tpl'); 
          }
      }
      
      public function executeAddTeam()
      {
        if(isset($_POST['submit']))
          {
              $obj = new Team();
              $obj->setPoulGroepid($_GET['param']);
              $obj->setNaam($_POST['name']);
              $obj->save();
              
              core::$tpl->assign('message', TEAM_ADDED_SUCCESFULL_MESSAGE);
              $this->executeIndex();  
          }
          else
          {
            $this->setContent('team/teamAddTeam.tpl'); 
          }  
      }
      
      public function executeEditGroup()
      {
          if(isset($_POST['submit']))
          {
              $obj = PoulGroepPeer::retrieveByPK($_GET['param']);
              $obj->setNaam($_POST['name']);
              $obj->save();
              
              core::$tpl->assign('message', EDIT_GROUP_SUCCESFULL_MESSAGE);
              $this->executeIndex();  
          }
          else
          {   
              core::$tpl->assign('group', PoulGroepPeer::retrieveByPK($_GET['param']));
              $this->setContent('team/teamEditGroup.tpl');
          }
      }      
      
      public function executeEditTeam()
      {
          if(isset($_POST['submit']))
          {
              $obj = TeamPeer::retrieveByPK($_GET['param']);
              $obj->setNaam($_POST['name']);
              $obj->save();
              
              core::$tpl->assign('message', EDIT_TEAM_SUCCESFULL_MESSAGE);
              $this->executeIndex();  
          }
          else
          {   
              core::$tpl->assign('team', TeamPeer::retrieveByPK($_GET['param']));
              $this->setContent('team/teamEditTeam.tpl');
          }
      }
      
      public function executeDeleteGroup()
      {
          $PoulGroep = PoulGroepPeer::retrieveByPK($_GET['param']);
          if($PoulGroep->countTeams()==0)
          {
                $PoulGroep->delete();
          }
          else
          {
              core::$tpl->assign( 'message', CANT_DELETE_GROUP_BECAUSE_TEAMS);
          }
          $this->executeIndex();
      }
      
      public function executeDeleteTeam()
      {
          $team = TeamPeer::retrieveByPK($_GET['param']);
          if($team->countWedstrijdsRelatedByThuisteam()==0 && $team->countWedstrijdsRelatedByUitteam()==0)
          {
                $team->delete();
          }
          else
          {
              core::$tpl->assign( 'message', CANT_DELETE_TEAM_BECAUSE_MATCH);
          }
          $this->executeIndex();
      }
      
      
  }
?>
